package com.demo.controller;

import com.demo.constant.MessageConstant;
import com.demo.dto.LeaveDTO;
import com.demo.dto.LeavePageQueryDTO;
import com.demo.pojo.Student;
import com.demo.result.PageResult;
import com.demo.result.Result;
import com.demo.service.LeaveService;
import com.demo.vo.LeaveVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping("/student/leave")
public class LeaveController {

    @Autowired
    LeaveService leaveService;

    /**
     * 新增假条
     * @param leaveDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody LeaveDTO leaveDTO, HttpSession session){
        Student student= (Student) session.getAttribute("student");
        System.out.println(student+"请假----请假");
        leaveDTO.setStuNo(student.getStuID());
        System.out.println("controller:"+leaveDTO);
        leaveService.save(leaveDTO);
        return Result.success();
    }

    /**
     * 删除假条
     * @param leaveID
     * @return
     */
    @DeleteMapping("/id")
    public Result delete(@RequestParam("leaveID") String leaveID){
        Boolean result=leaveService.delete(leaveID);
        System.out.println(result);
        if (result){
            return Result.success();
        }
        return Result.error(MessageConstant.LEAVE_APPROVE);
    }

    /**
     * 分页查询
     * @param leavePageQueryDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody LeavePageQueryDTO leavePageQueryDTO,HttpSession session){
        System.out.println("没有值++++++++++++");
        Student student=(Student) session.getAttribute("student");

        System.out.println(leavePageQueryDTO+"没有值呀");
        PageResult pageResult=leaveService.pageQuery(leavePageQueryDTO);
        System.out.println(pageResult+"..............");
        return Result.success(pageResult);
    }

    /**
     * 辅导员审批假条
     * @param leaveDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody LeaveDTO leaveDTO){
        System.out.println(leaveDTO);
        leaveService.update(leaveDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<LeaveVO> selectByLeaveID(String leaveID){
        leaveService.selectByLeaveID(leaveID);
        return Result.success();
    }

    @PostMapping("/exprot")
    public ResponseEntity<byte[]> exportData(@RequestBody Map<String, String> params) throws JsonProcessingException {
        return leaveService.exportData(params);
    }

}
